{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorials of retail-kit \n",
    "\n",
    "This is a simple tutorials of retail-kit which will help you get faster start on this module. Firstly, you should install this module to your enveriment as install commands. \n",
    "```bash\n",
    "git clone git@gitlab.ainnovation.com:Retail/Algorithm/retail-kit.git\n",
    "cd retail-kit\n",
    "python setup.py install\n",
    "```\n",
    "Then, test it.\n",
    "```bash\n",
    "python -c \"from odutils import sku_utils; print(sku_utils.get_uuid_name('hello', 'hello'));\"\n",
    "```\n",
    "You will get `hello` in command line if you have succussfully installed out module. Let's begin our explor jounery."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## common\n",
    "\n",
    "Some common functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fc3ff98e8c6a0d3087d515c0473f8677'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from odutils import common\n",
    "\n",
    "common.str_md5(b\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'78ef68043f5aed0de916c936e3d8fb2f'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "\n",
    "common.image_md5(Image.new(\"RGB\", [100, 100]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['kkk/002.jpg', 'abc/001.jpg'], ['ttt/002.xml', 'abc/001.xml']]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "paths0 = [\"abc/001.jpg\",\n",
    "          \"kkk/002.jpg\",\n",
    "          \"hhh/003.jpg\"]\n",
    "paths1 = [\"abc/001.xml\",\n",
    "          \"ttt/002.xml\",\n",
    "          \"hhh/004.xml\"]\n",
    "\n",
    "common.align_paths(paths0, paths1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/8dc540049b2c737ffb752ba3a4b33e77.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/b267b29ea70e7d52e59315590114d0c4.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/3c5f00b8bbc99b778a09e7d1d57f5cd0.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/c49e51b45bc22cf1966ef491de069683.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/029e362023611e50bbdccfd8fd4cb5af.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/72bf040ea59f844c604fca0d3b2120cf.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/508448385daebfc5fa2f2dd2959c1565.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/88cced87b7460e8391403d10dede0d52.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/209fd496066a48f8cefb24fbfa5893bd.xml',\n",
       " '/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/annotations/03fd0ddaa1af7e5511dcd2185f789097.xml']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "common.find_file_recursive(\"/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/\", \"xml\")[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object ifind_file_recursive at 0x7f2d155eb8e0>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "common.ifind_file_recursive(\"/home/zhaojh/Datasets/carlsberg/yunnan_UAT_20190512/\", \"xml\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sku_utils\n",
    "\n",
    "Helper for sku information lookup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "from odutils import sku_utils\n",
    "\n",
    "print(sku_utils.get_uuid_name('hello', 'hello'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. get single sku info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "uuid info:\n",
      " {'id': 3035, 'name': '竞品啤酒-瓶装-嘉士伯', 'type_id': 9, 'pinyin': 'jppj-pz-jsb', 'uuid': '37a448d8-3f10-11e9-8620-0242cb7ccd7c', 'image': 'sku_default.jpeg', 'type_name': '瓶装'}\n",
      "\n",
      "uuid name:\n",
      " 竞品啤酒-瓶装-嘉士伯\n",
      "\n",
      "uuid pinyin:\n",
      " jppj-pz-jsb\n",
      "\n",
      "uuid type:\n",
      " pz\n"
     ]
    }
   ],
   "source": [
    "sku_uuid = \"37a448d8-3f10-11e9-8620-0242cb7ccd7c\"\n",
    "\n",
    "# get uuid info\n",
    "info = sku_utils.get_uuid_info(sku_uuid)\n",
    "print(\"uuid info:\\n\", info)\n",
    "print()\n",
    "\n",
    "# get uuid name\n",
    "name = sku_utils.get_uuid_name(sku_uuid)\n",
    "print(\"uuid name:\\n\", name)\n",
    "print()\n",
    "\n",
    "# get uuid pinyin\n",
    "pinyin = sku_utils.get_uuid_pinyin(sku_uuid)\n",
    "print(\"uuid pinyin:\\n\", pinyin)\n",
    "print()\n",
    "\n",
    "# get uuid type\n",
    "typ = sku_utils.get_uuid_type(sku_uuid)\n",
    "print(\"uuid type:\\n\", typ)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. get a set skus' info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'37a448d8-3f10-11e9-8620-0242cb7ccd7c': {'id': 3035,\n",
       "  'name': '竞品啤酒-瓶装-嘉士伯',\n",
       "  'type_id': 9,\n",
       "  'pinyin': 'jppj-pz-jsb',\n",
       "  'uuid': '37a448d8-3f10-11e9-8620-0242cb7ccd7c',\n",
       "  'image': 'sku_default.jpeg',\n",
       "  'type_name': '瓶装'},\n",
       " 'd9237a5a-9d59-11e8-a9ec-02422fc40004': {'id': 396,\n",
       "  'name': '凯旋K1664白啤_瓶_330ml',\n",
       "  'type_id': 9,\n",
       "  'pinyin': 'kxK1664bp_p_330ml',\n",
       "  'uuid': 'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
       "  'image': 'http://v1-static.oss-cn-zhangjiakou.aliyuncs.com/sv_unified/sku/d3ad355b1d435d122c236877e7d6f816.jpg',\n",
       "  'type_name': '瓶装'},\n",
       " 'bd1312a4-b337-11e8-a718-0242cb74f9f9': {'id': 1170,\n",
       "  'name': '百威啤酒_瓶_600ml',\n",
       "  'type_id': 9,\n",
       "  'pinyin': 'bwpj_p_600ml',\n",
       "  'uuid': 'bd1312a4-b337-11e8-a718-0242cb74f9f9',\n",
       "  'image': 'http://v1-static.oss-cn-zhangjiakou.aliyuncs.com/sv_unified/sku/bd83060f4393e622a69f4bf70e7a5fd5.jpg',\n",
       "  'type_name': '瓶装'}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uuids = ['37a448d8-3f10-11e9-8620-0242cb7ccd7c',\n",
    "         '37a448d8-3f10-11e9-8620-0242cb7ccd7c',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'd9237a5a-9d59-11e8-a9ec-02422fc40004',\n",
    "         'bd1312a4-b337-11e8-a718-0242cb74f9f9',\n",
    "         'bd1312a4-b337-11e8-a718-0242cb74f9f9',\n",
    "         'bd1312a4-b337-11e8-a718-0242cb74f9f9']\n",
    "\n",
    "sku_utils.get_uuid2infos_map(uuids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'37a448d8-3f10-11e9-8620-0242cb7ccd7c': '竞品啤酒-瓶装-嘉士伯',\n",
       " 'd9237a5a-9d59-11e8-a9ec-02422fc40004': '凯旋K1664白啤_瓶_330ml',\n",
       " 'bd1312a4-b337-11e8-a718-0242cb74f9f9': '百威啤酒_瓶_600ml'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sku_utils.get_uuid2name_map(uuids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'37a448d8-3f10-11e9-8620-0242cb7ccd7c': 'jppj-pz-jsb',\n",
       " 'd9237a5a-9d59-11e8-a9ec-02422fc40004': 'kxK1664bp_p_330ml',\n",
       " 'bd1312a4-b337-11e8-a718-0242cb74f9f9': 'bwpj_p_600ml'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sku_utils.get_uuid2pinyin_map(uuids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'37a448d8-3f10-11e9-8620-0242cb7ccd7c': 'pz',\n",
       " 'd9237a5a-9d59-11e8-a9ec-02422fc40004': 'pz',\n",
       " 'bd1312a4-b337-11e8-a718-0242cb74f9f9': 'pz'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sku_utils.get_uuid2type_map(uuids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
